<template>
    <div class="system-sys-client">
        <el-card v-show="showSearch" class="search-card">
            <el-form @submit.native.prevent :model="queryParams" ref="queryForm" :inline="true" label-width="85px" class="search-form">
                <el-form-item prop="clientKey">
                    <el-input v-model="queryParams.clientKey" :placeholder="$t('system.sysclient.652154-1')" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
                <div style="float: right">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('system.sysclient.652154-2') }}</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('system.sysclient.652154-3') }}</el-button>
                </div>
            </el-form>
        </el-card>

        <el-card>
            <el-row :gutter="10" style="margin-bottom: 16px">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd" v-hasPermi="['system:sysclient:add']">{{ $t('system.sysclient.652154-4') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate" v-hasPermi="['system:sysclient:edit']">{{ $t('system.sysclient.652154-5') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:sysclient:remove']">{{ $t('system.sysclient.652154-6') }}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button plain icon="el-icon-download" size="small" @click="handleExport" v-hasPermi="['system:sysclient:export']">{{ $t('system.sysclient.652154-7') }}</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table v-loading="loading" :data="sysclientList" @selection-change="handleSelectionChange" :border="false">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column :label="$t('system.sysclient.652154-8')" align="left" prop="clientKey" min-width="200" />
                <el-table-column :label="$t('system.sysclient.652154-9')" align="left" prop="clientSecret" min-width="200" />
                <el-table-column :label="$t('system.sysclient.652154-10')" align="left" prop="token" min-width="410" />
                <el-table-column :label="$t('system.sysclient.652154-13')" align="center" prop="timeout" width="120" />
                <el-table-column :label="$t('system.sysclient.652154-14')" align="center" prop="enable" width="100">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.enable" active-value="1" inactive-value="0" @change="handleEnableChange(scope.row)" :disabled="isDisabled"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" :label="$t('system.sysclient.652154-15')" align="center" class-name="small-padding fixed-width" width="140">
                    <template slot-scope="scope">
                        <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:sysclient:edit']">{{ $t('system.sysclient.652154-5') }}</el-button>
                        <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:sysclient:remove']">{{ $t('system.sysclient.652154-6') }}</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination style="margin-bottom: 20px" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改系统授权对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
                <el-form-item :label="$t('system.sysclient.652154-16')" prop="clientKey">
                    <el-input v-model="form.clientKey" :placeholder="$t('system.sysclient.652154-17')" style="width: 400px" />
                </el-form-item>
                <el-form-item :label="$t('system.sysclient.652154-18')" prop="clientSecret">
                    <el-input v-model="form.clientSecret" :placeholder="$t('system.sysclient.652154-19')" style="width: 400px" />
                </el-form-item>
                <el-form-item :label="$t('system.sysclient.652154-20')" prop="timeout">
                    <el-input v-model="form.timeout" :placeholder="$t('system.sysclient.652154-21')" type="number" oninput="if(value>99999999)value=99999999;if(value<0)value=0" style="width: 280px">
                        <template slot="append">{{ $t('system.sysclient.652154-21') }}</template>
                    </el-input>
                    <span style="margin-left: 10px">
                        <el-tooltip class="item" effect="dark" :content="$t('system.sysclient.652154-22')" placement="top">
                            <i class="el-icon-question"></i>
                        </el-tooltip>
                    </span>
                </el-form-item>
                <el-form-item :label="$t('system.sysclient.652154-23')" prop="enable">
                    <el-switch v-model="form.enable" active-value="1" inactive-value="0" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">{{ $t('system.sysclient.652154-24') }}</el-button>
                <el-button @click="cancel">{{ $t('system.sysclient.652154-25') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listSysclient, getSysclient, delSysclient, addSysclient, updateSysclient } from '@/api/system/sysclient';

export default {
    name: 'Sysclient',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            //防止一直点击是否生效按钮
            isDisabled: false,
            // 总条数
            total: 0,
            // 系统授权表格数据
            sysclientList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                clientKey: null,
                clientSecret: null,
                token: null,
                grantType: null,
                deviceType: null,
                timeout: null,
                enable: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                clientKey: [
                    {
                        required: true,
                        message: this.$t('system.sysclient.652154-17'),
                        trigger: 'blur',
                    },
                ],
                clientSecret: [
                    {
                        required: true,
                        message: this.$t('system.sysclient.652154-19'),
                        trigger: 'blur',
                    },
                    {
                        validator: (rule, value, callback) => {
                            if (/[a-zA-z]$/.test(value) == false) {
                                callback(new Error(this.$t('system.sysclient.652154-33')));
                            } else {
                                callback();
                            }
                        },
                        trigger: 'blur',
                    },
                ],
                timeout: [
                    {
                        required: true,
                        message: this.$t('system.sysclient.652154-32'),
                        trigger: 'blur',
                    },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询系统授权列表 */
        getList() {
            this.loading = true;
            listSysclient(this.queryParams).then((response) => {
                this.sysclientList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                clientKey: null,
                clientSecret: null,
                token: null,
                grantType: null,
                deviceType: null,
                timeout: null,
                enable: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = this.$t('system.sysclient.652154-26');
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getSysclient(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = this.$t('system.sysclient.652154-27');
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateSysclient(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('system.sysclient.652154-28'));
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSysclient(this.form).then((response) => {
                            this.$modal.msgSuccess(this.$t('system.sysclient.652154-29'));
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm(this.$t('system.sysclient.652154-31', [ids]))
                .then(function () {
                    return delSysclient(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('system.sysclient.652154-30'));
                })
                .catch(() => {});
        },
        async handleEnableChange(row) {
            this.isDisabled = true; // 设置为不可选择
            /**简单防抖 */
            setTimeout(() => {
                this.isDisabled = false; // 2秒后恢复可选择状态
            }, 1000);
            this.reset();
            updateSysclient(row).then((response) => {
                this.$modal.msgSuccess(this.$t('system.sysclient.652154-28'));
                this.getList();
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'system/sysclient/export',
                {
                    ...this.queryParams,
                },
                `sysclient_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>

<style lang="scss" scoped>
.system-sys-client {
    padding: 20px;

    .search-card {
        margin-bottom: 15px;
        padding: 3px 0;
    }

    .search-form {
        margin-bottom: -22.5px;
    }
}
</style>
